package com.inno.gateway.config;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author sean
 */
@Configuration
public class SaTokenConfigure {
    // 注册 Sa-Token全局过滤器
    @Bean
    public SaReactorFilter getSaReactorFilter() {
        return new SaReactorFilter()
                // 拦截地址
                .addInclude("/**")
                // 开放地址
                .addExclude("/auth/login")
                .addExclude("/auth/mp-login")
                .addExclude("/auth/mobile-login")
                .addExclude("/oss/upload")
                .addExclude("/auth/send-register-sms")
                .addExclude("/auth/register")
                .addExclude("/auth/check")
                .addExclude("/auth/adminLogin")
                .addExclude("/subject/category/queryList")
                .addExclude("/subject/type/queryList")
                .addExclude("/subject/level/queryList")
                .addExclude("/system/banner/mp-list")
                .addExclude("/subject/info/queryPage")
                .addExclude("/subject/info/queryById")
                .addExclude("/subject/rank/getRankInfo")
                .addExclude("/subject/info/daily")
                .addExclude("/life/moment/queryPage")
                .addExclude("/life/moment/comment/list")
                .addExclude("/life/moment/queryById")
                .addExclude("/system/news/queryList")
                // 鉴权方法：每次访问进入
                .setAuth(obj -> {
                    // 登录校验 -- 拦截所有路由，并排除/user/doLogin 用于开放登录
                    SaRouter.match("/**", "/auth/login", r -> StpUtil.checkLogin());
                    SaRouter.match("/**", "/auth/send-register-sms", r -> StpUtil.checkLogin());
                    SaRouter.match("/**", "/auth/register", r -> StpUtil.checkLogin());
                    SaRouter.match("/life/**",r->StpUtil.checkLogin());
//                    // 角色认证 -- 拦截以 admin 开头的路由，必须具备 admin 角色或者 super-admin 角色才可以通过认证
//                    SaRouter.match("/admin/**", r -> StpUtil.checkRoleOr("admin", "super-admin"));
//                    // 权限认证 -- 不同模块, 校验不同权限
//                    SaRouter.match("/meta-system/**", r -> StpUtil.checkPermission("system-no"));
//                    SaRouter.match("/admin/**", r -> StpUtil.checkPermission("admin"));
//                    SaRouter.match("/goods/**", r -> StpUtil.checkPermission("goods"));
//                    SaRouter.match("/orders/**", r -> StpUtil.checkPermission("orders"));
                })
                // 异常处理方法：每次setAuth函数出现异常时进入

                .setBeforeAuth(obj -> {
                    // ---------- 设置跨域响应头 ----------
                    SaHolder.getResponse()
                            // 允许指定域访问跨域资源
                            .setHeader("Access-Control-Allow-Origin", "*")
                            // 允许所有请求方式
                            .setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
                            // 有效时间
                            .setHeader("Access-Control-Max-Age", "3600")
                            // 允许的header参数
                            .setHeader("Access-Control-Allow-Headers", "*");

                    // 如果是预检请求，则立即返回到前端
                    SaRouter.match(SaHttpMethod.OPTIONS)
                            .free(r -> System.out.println("--------OPTIONS预检请求，不做处理"))
                            .back();
                });

    }



}
